package com.crcc.common.util;

import com.crcc.common.constant.CommonConsts;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ExcelZipUtil {

    public static void getHSSFWorkbookExcleZip( List<HSSFWorkbook>  hssfWorkbookList, List<String>  nameList,OutputStream out, Set<String> projectidNameSet, List<String> projectidName) throws Exception {
        ZipOutputStream zipOutputStream = new ZipOutputStream(out);
        for (String s :projectidNameSet){
            for (int i = CommonConsts.ZERO;i<projectidName.size();i++){
                if (s.equals(projectidName.get(i))){
                    ZipEntry entry = new ZipEntry(s+"/"+nameList.get(i)+".xlsx");
                    HSSFWorkbook hssfWorkbook = hssfWorkbookList.get(i);
                    //zip中文件
                    zipOutputStream.putNextEntry(entry);
                    hssfWorkbook.write(zipOutputStream);
                }
            }
        }
        zipOutputStream.flush();
        zipOutputStream.close();
    }

    /***
     * 按照公司-项目-进行创建文件夹下载压缩包
     * @param hssfWorkbookList
     * @param out
     * @param projectidNameSet
     * @param cpListVOS
     * @throws Exception
     */
    public static void getHSSFWorkbookExcleZipNew(List<HSSFWorkbook>  hssfWorkbookList, OutputStream out, List<Map<String,Object>> projectidNameSet, List<Map<String,Object>> cpListVOS) throws Exception {
        ZipOutputStream zipOutputStream = new ZipOutputStream(out);
        for (Map<String,Object> s :cpListVOS){
            List<Map<String,String>> list  = (List<Map<String,String>>) s.get("list");
            for(int y = CommonConsts.ZERO;y<list.size();y++){
                for (int i = CommonConsts.ZERO;i<projectidNameSet.size();i++){
                    if(projectidNameSet.get(i).get("projectName").toString().equals(String.valueOf(list.get(y).get("projectName")))){
                       if(projectidNameSet.get(i).get("cId").toString().equals(String.valueOf(list.get(y).get("cId")))){
                           String newName = reSpecialWord(list.get(y).get("projectName"));
                           ZipEntry entry = new ZipEntry(s.get("cName").toString()+"/"+newName+"/"+projectidNameSet.get(i).get("name")+System.currentTimeMillis()+".xlsx");
                           HSSFWorkbook hssfWorkbook = hssfWorkbookList.get(i);
                           //zip中文件
                           zipOutputStream.putNextEntry(entry);
                           hssfWorkbook.write(zipOutputStream);
                        }
                    }
                }
            }
        }
        zipOutputStream.flush();
        zipOutputStream.close();
    }

    public static void getHSSFWorkbookEvaluateExcle( List<HSSFWorkbook>  hssfWorkbookList, List<String> nameList,OutputStream out) throws Exception {
        ZipOutputStream zipOutputStream = new ZipOutputStream(out);
            for (int i = CommonConsts.ZERO;i<nameList.size();i++){
                String newName = reSpecialWord(nameList.get(i));
                //如果文件名中有特殊符号，替换为-
                ZipEntry entry = new ZipEntry(newName+".xlsx");
                HSSFWorkbook hssfWorkbook = hssfWorkbookList.get(i);
                //zip中文件
                zipOutputStream.putNextEntry(entry);
                hssfWorkbook.write(zipOutputStream);
            }
        zipOutputStream.flush();
        zipOutputStream.close();
    }

    public static void getCredentialsTemplateZip( String xwbName,HSSFWorkbook  hssfWorkbook, List<String> nameList,OutputStream out) throws Exception {
        ZipOutputStream zipOutputStream = new ZipOutputStream(out);
        ZipEntry entry = new ZipEntry(xwbName+".xls");
        //zip中文件
        zipOutputStream.putNextEntry(entry);
        hssfWorkbook.write(zipOutputStream);


        for (int i = CommonConsts.ZERO;i<nameList.size();i++){
            //添加空文件夹
            zipOutputStream.putNextEntry(new ZipEntry(nameList.get(i)+"/"));
        }
        zipOutputStream.flush();
        zipOutputStream.close();
    }

    /**
     * 如果文件名中有特殊符号，替换为-
     * @param name
     * @return
     */
    public static String reSpecialWord(String name){
        String newName = name;
        if(name.indexOf("/")>-1){
            newName = name.replace("/", "-");
        }
        if(name.indexOf("\\")>-1){
            newName = name.replace("\\", "-");
        }
        if(name.indexOf(":")>-1){
            newName = name.replace(":", "-");
        }
        if(name.indexOf("*")>-1){
            newName = name.replace("*", "-");
        }
        if(name.indexOf("?")>-1){
            newName = name.replace("?", "-");
        }
        if(name.indexOf("\"")>-1){
            newName = name.replace("\"", "-");
        }
        if(name.indexOf("<")>-1){
            newName = name.replace("<", "-");
        }
        if(name.indexOf(">")>-1){
            newName = name.replace(">", "-");
        }
        if(name.indexOf("|")>-1){
            newName = name.replace("|", "-");
        }
        return newName;
    }

    public static void getPdfWriterZip(List<File>  files, OutputStream out, Set<String> projectidNameSet, List<String> projectidName) throws Exception {
        ZipOutputStream zipOutputStream = new ZipOutputStream(out);
        ZipEntry ze = null;
        for (String pn :projectidNameSet){
            for (int i = CommonConsts.ZERO;i<projectidName.size();i++){
                if (pn.equals(projectidName.get(i))){
                    BufferedInputStream bis = new BufferedInputStream(
                            new FileInputStream(files.get(i)));
                    ze = new ZipEntry(pn+"/"+files.get(i).getName());
                    zipOutputStream.putNextEntry(ze);
                    int s = -1;
                    while ((s = bis.read()) != -1) {
                        zipOutputStream.write(s);
                    }
                    bis.close();
                    files.get(i).delete();
                }
            }
        }
        zipOutputStream.flush();
        zipOutputStream.close();
    }

}
